// order-list.js
var app = getApp();
var iFDU = require('../../utils/iFDU.js');
var sliderWidth = 75;
Page({
  data: {
    config: app.config,
    globalData: app.globalData,

    tabs: ["全部", "待支付", "待发货", "已发货", "已完成"],
    activeIndex: 0,
    sliderOffset: 0,
    sliderLeft: 0,
    screenWidth: 0,
    screenHeight: 0,
    orderStatus: {
      1: '待支付', 
      2: '全速定制中...', 
      3: '已发货', 
      5: '已完成,谢谢!', 
      13: '用户取消', 
      15: '系统取消'
    },
    orders: [],
    loaded: false,
  },

  onLoad: function (options) {
    var screenWidth = wx.getSystemInfoSync().screenWidth;
    var screenHeight = wx.getSystemInfoSync().screenHeight;
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          sliderLeft: (res.windowWidth / that.data.tabs.length - sliderWidth) / 2,
          sliderOffset: res.windowWidth / that.data.tabs.length * that.data.activeIndex,
          screenWidth: screenWidth,
          screenHeight: screenHeight
        });
      }
    });
  },

  onShow: function () {
    var that = this;
    wx.showLoading({
      title: '加载中',
    });
    app.getUserInfo(function (globalData) {
      that.setData({
        globalData: globalData
      });
      that.loadOrders();
    });
  },

  loadOrders: function () {
    var that = this;
    iFDU.api.doGet(
      'order/getOrderList',
      {
        userId: that.data.globalData.userId,
        type: that.data.activeIndex,
      },
      function (data) {
        var orders = data;
        if (orders && orders.length > 0) {
          for (var i = 0; i < orders.length; i++) {
            var orderItems = orders[i].orderDetailViewList;
            var needCustomize = false;
            if (orderItems && orderItems.length > 0) {
              for (var j = 0; j < orderItems.length; j++) {
                var orderItem = orderItems[j];
                var product = orderItem.mainProductView;
                if (product && product.is_virtual == 0 && product.is_broider == 1) {
                  needCustomize = true;
                  break;
                }
              }
            }
            orders[i].needCustomize = needCustomize;
          }
        }
        that.setData({
          loaded: true,
          orders: orders
        });
        wx.hideLoading();
      }
    );
  },

  tabClick: function (e) {
    wx.showLoading({
      title: '加载中',
    });
    this.setData({
      sliderOffset: e.currentTarget.offsetLeft,
      activeIndex: e.currentTarget.id
    });

    this.loadOrders();
  },

  showOrderDetail: function(e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../order-detail/order-detail?orderId=' + id,
    })
  },

  showCSTips: function () {
    wx.showModal({
      content: '如果您需要联系我们或售后问题，请在服务号中发起会话，或添加逸见服饰小秘书微信进行及时沟通！微信号：yjshirt',
      showCancel: false,
      confirmText: "知道了",
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定')
        }
      }
    });
  },
  cancelOrder: function (e) {
    var that = this;
    wx.showModal({
      title: '取消订单',
      content: '确定取消此订单吗？',
      confirmText: "取消订单",
      cancelText: "先不取消",
      success: function (res) {
        if (res.confirm) {
          wx.showLoading({
            title: '加载中',
          });
          iFDU.api.doGet(
            'order/cancelOrder',
            {
              userId: that.data.globalData.userId,
              orderId: e.currentTarget.dataset.id,
            },
            function (data) {
              that.loadOrders();
            }
          );
        }
      }
    });
  },
  deleteOrder: function (e) {
    var that = this;
    wx.showModal({
      title: '删除订单',
      content: '将此订单从列表中永久删除吗？',
      confirmText: "删除",
      cancelText: "保留",
      success: function (res) {
        if (res.confirm) {
          wx.showLoading({
            title: '加载中',
          });
          iFDU.api.doGet(
            'order/deleteOrder',
            {
              userId: that.data.globalData.userId,
              orderId: e.currentTarget.dataset.id,
            },
            function (data) {
              that.loadOrders();
            }
          );
        }
      }
    });
  },

  viewExpress: function (e) {
    var deliverCompany = e.currentTarget.dataset.deliver_company;
    var deliverOrderId = e.currentTarget.dataset.deliver_order_id;
    wx.navigateTo({
      url: '../trace-detail/trace-detail?deliverCompany=' + deliverCompany + '&deliverOrderId=' + deliverOrderId,
    })
  },
  
  payOrder: function (e) {
    var that = this;
    var orderId = e.currentTarget.dataset.id;
    var data = {
      userId: that.data.globalData.userId,
      orderId: orderId,
    };
    iFDU.api.doGet('payment/getInfoForPayment', data, function (data) {
      wx.navigateTo({
        url: '../goods-pay/goods-pay?orderId=' + orderId,
      })
    });
  },

  goHome: function () {
    wx.switchTab({
      url: '../home/home',
    })
  },

  buyAgain: function (e) {
    // var that = this;
    // var index = e.currentTarget.dataset.idx;
    // var order = that.data.orders[index];
    // var goodsList = order.orderDetailViewList;
    // var isVirtual = false;
    // var datas = [];
    // for (var i in goodsList) {
    //   var goodsDetail = goodsList[i];
    //   if (goodsDetail.mainProductView.is_virtual == 1) {
    //     isVirtual = true;
    //     break;
    //   }
    //   var data = {
    //     userId: that.data.globalData.userId,
    //     mainProductId: goodsDetail.goods_id,
    //     count: 1
    //   };
    //   var hasSku = goodsDetail.orderDetailSkuList && goodsDetail.orderDetailSkuList.length > 0;
    //   if (hasSku) {
    //     var selectedSkuIds = that.calcSeletedSkuIds();
    //     var selectedSubSkuIds = [];
    //     for (var i in selectedSkuIds) {
    //       selectedSubSkuIds.push(selectedSkuIds[i].split('-')[1]);
    //     }
    //     data['subSkuIds'] = selectedSubSkuIds.join(',');
    //   }
    //   if (that.data.selectedmarketingProductId != 0) {
    //     data['subMarketingProductIds'] = that.data.selectedmarketingProductId
    //   }
    //   datas.push(data);
    // }
    // if (isVirtual) {

    // } else {

    // }
  },

  onShareAppMessage: function (res) {
    var that = this;
    return {
      title: '爱上衬衫，从「逸见」开始…',
      imageUrl: 'http://s.i-fdu.com/runto/wxa/s/yjshare.jpeg',
      path: 'pages/home/home',
      success: function (res) {
      },
      fail: function (res) {
      }
    }
  }
})